/*
 * Copyright @ 2016QIANLONG.
 * All right reserved.
 */

package com.pay.modules.alipay.controller;
import com.pay.payment.service.IPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("callback/credit")
public class AlipayCreditCallController {

	@Autowired
	IPayService payService;

	private static final Logger logger = LoggerFactory.getLogger(AlipayCreditCallController.class);

	@RequestMapping(value = "alipay")
	public void alipay(HttpServletResponse response, HttpServletRequest request) {
	try {

		Map<String, Object> map = new HashMap<String, Object>();
		Enumeration paramNames = request.getParameterNames();
		while(paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				if ( paramValues[0].length() != 0) {
					map.put(paramName,  paramValues[0]);
				}
			}
		}
		logger.info("==========[callBack.allinpay]======request ={}",map);
		String  seqNo = map.get("out_order_no").toString();
		String status = map.get("status").toString();
		logger.info("==========[callBack.allinpay]======seqNo ={},status ={}",seqNo,status);
		if(status.equals("SUCCESS")){
			String  authNo = map.get("auth_no").toString();
			String  payerUserId = map.get("payer_user_id").toString();
			//支付状态(0= 申请成功 1=授权支付成功 2=授权支付中 3=授权支付失败 4=授权解冻中 5=授权解冻失败 6=授权解冻成功 7申请失败 8支付成功)
			Boolean i =	payService.callBackSucceedPay(seqNo,authNo,payerUserId,1);
			if(i){
				logger.info("==========[callBack.allinpay]======seqNo ={},status ={},状态处理成 成功",seqNo,status);
			}
		}else {
			Boolean i =	payService.callBackPay(seqNo,3);
			if(i){
				logger.info("==========[callBack.allinpay]======seqNo ={},status ={}状态处理成 失败",seqNo,status);
			}
		}
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
			writer.write("success"); //一定要打印success
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
		}

	}catch (Exception e) {
		logger.info("==========[callBack.allinpay]======Exception ={}",e);
	}
	}



	@RequestMapping(value = "fundAuthOrderUnFreeze")
	public void fundAuthOrderUnFreeze(HttpServletResponse response, HttpServletRequest request) {
		try {

			Map<String, Object> map = new HashMap<String, Object>();
			Enumeration paramNames = request.getParameterNames();
			while(paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String[] paramValues = request.getParameterValues(paramName);
				if (paramValues.length == 1) {
					if ( paramValues[0].length() != 0) {
						map.put(paramName,  paramValues[0]);
					}
				}
			}
			logger.info("==========[callBack.fundAuthOrderUnFreeze]======request ={}",map);
			String  seqNo = map.get("out_order_no").toString();
			String status = map.get("status").toString();
			logger.info("==========[callBack.fundAuthOrderUnFreeze]======seqNo ={},status ={}",seqNo,status);
			if(status.equals("SUCCESS")){
				//支付状态(0= 申请成功 1=授权支付成功 2=授权支付中 3=授权支付失败 4=授权解冻中 5=授权解冻失败 6=授权解冻成功 7申请失败 8支付成功)
				Boolean i =	payService.callBackRefundPay(seqNo,4,6,new Date());
				if(i){
					logger.info("==========[callBack.fundAuthOrderUnFreeze]======seqNo ={},status ={},状态处理成 授权解冻成功",seqNo,status);
				}
			}else {
				Boolean i =	payService.completePay(seqNo,4,5);
				if(i){
					logger.info("==========[callBack.fundAuthOrderUnFreeze]======seqNo ={},status ={}状态处理成 授权解冻失败",seqNo,status);
				}
			}
			PrintWriter writer = null;
			try {
				writer = response.getWriter();
				writer.write("success"); //一定要打印success
				writer.flush();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (writer != null) {
					writer.close();
				}
			}
		}catch (Exception e) {
			logger.info("==========[callBack.fundAuthOrderUnFreeze]======Exception ={}",e);
		}
	}

	@RequestMapping(value = "tradePay")
	public void tradePay(HttpServletResponse response, HttpServletRequest request) {
		try {

			Map<String, Object> map = new HashMap<String, Object>();
			Enumeration paramNames = request.getParameterNames();
			while(paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String[] paramValues = request.getParameterValues(paramName);
				if (paramValues.length == 1) {
					if ( paramValues[0].length() != 0) {
						map.put(paramName,  paramValues[0]);
					}
				}
			}
			logger.info("==========[callBack.tradePay]======request ={}",map);
			String  seqNo = map.get("out_order_no").toString();
			String status = map.get("status").toString();
			logger.info("==========[callBack.tradePay]======seqNo ={},status ={}",seqNo,status);
			if(status.equals("SUCCESS")){
				//支付状态(0= 申请成功 1=授权支付成功 2=授权支付中 3=授权支付失败 4=授权解冻中 5=授权解冻失败 6=授权解冻成功 7申请失败 8支付成功)
				boolean i =	payService.callBackCompletePay(seqNo,2,8,new Date());
				if(i){
					logger.info("==========[callBack.tradePay]======seqNo ={},status ={},支付成功 成功",seqNo,status);
				}
			}else {
				boolean i =	payService.completePay(seqNo,2,3);
				if(i){
					logger.info("==========[callBack.tradePay]======seqNo ={},status ={}状态处理成 授权支付失败",seqNo,status);
				}
			}

			PrintWriter writer = null;
			try {
				writer = response.getWriter();
				writer.write("success"); //一定要打印success
				writer.flush();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (writer != null) {
					writer.close();
				}
			}

		}catch (Exception e) {
			logger.info("==========[callBack.tradePay]======Exception ={}",e);
		}

	}

}
